<template>
	<view class="echrts_content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view @click="echarts.onClick" :prop="optionData" :style="{width:moduleParam.width,height:moduleParam.height}"
			:change:prop="echarts.updateEcharts" :moduleParam="moduleParam" :change:moduleParam="echarts.upmoduleParam"
			:id="moduleParam.id" class="echarts"></view>
		<!-- <button @click="changeOption">更新数据</button> -->
		<!-- #endif -->
		<!-- #ifndef APP-PLUS || H5 -->
		<view>非 APP、H5 环境不支持</view>
		<!-- #endif -->
	</view>
</template>
<script>
	export default {
		name: "com-charts",
		props: {
			moduleParam: {
				type: Object,
				default: () => {
					id: "myCharts";
					width: "100%";
					height: "300rpx"
				}
			},
			optionData: {
				type: Object,
				default: () => {}
			}
		},
		data() {
			return {

			}
		},
		mounted() {

		},
		methods: {
			changeHeight(options) {
				this.moduleParam.height = options.height;
			},
			changeOption() {
				// 父组件刷新数据
				this.$emit("changeOption")
			},
			changecharts(index) {
				this.$emit("changecharts", {
					xIndex: index
				})
			},
			/* 渲染出错了 */
			renderingerrors(data) {
				console.log(data);
				console.log("=============================出错回调");
			},
			onViewClick(options) {
				this.$emit("getClickData", options)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	var myChart;
	export default {
		data() {
			return {
				clickData: null,
				moduleParamEcharts: {},
				optionDataEcharts: {}
			}
		},
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				let _this = this;
				/* var timer = setInterval(() => {
					if(this.moduleParam.id) {
						clearInterval(timer);
					}
					clearInterval(timer);
				}, 1000) */
				let moduleParam = this.moduleParamEcharts;
				let optionData = this.optionDataEcharts
				myChart = echarts.init(document.getElementById(moduleParam.id))
				if (optionData) {
					// 观测更新的数据在 view 层可以直接访问到
					myChart.setOption(optionData)
				} else {
					console.log("=======================出错了");
					this.$ownerInstance.callMethod('renderingerrors', moduleParam)
				}
				// 点击传参
				myChart.on('click', params => {
					this.clickData = params
				})
				if (this.moduleParamEcharts && this.moduleParamEcharts.id == "moId8") {
					myChart.getZr().off('click');
					myChart.getZr().on('click', params => {
						this.clickData = params
						let pointInPixel = [params.offsetX, params.offsetY]
						if (myChart.containPixel('grid', pointInPixel)) {
							//点击第几个柱子
							let pointInGrid = myChart.convertFromPixel({
								seriesIndex: 0
							}, pointInPixel)
							// x轴数据的索引
							let xIndex = pointInGrid[0]
							// y轴数据的索引
							let yIndex = pointInGrid[1]
							this.$ownerInstance.callMethod('changecharts', xIndex)
						}
					})
				}

			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				this.optionDataEcharts = newValue;
				if (this.moduleParamEcharts && this.moduleParamEcharts.id == "moId1" && !this.optionDataEcharts.series[1]
					.itemStyle) {
					console.log("来了===================================");
					this.optionDataEcharts.series[1].itemStyle = {
						normal: {
							color: function(params) {
								return new echarts.graphic.LinearGradient(
									0, 0, 1, 1, [{
											offset: 0,
											color: 'rgba(128, 165, 255, 0.28)'
										},
										{
											offset: 1,
											color: 'rgba(0, 74, 255, 0.28)'
										}
									]
								)
							}
						}
					}
				}
				if (oldValue) {
					// 监听 service 层数据变更
					myChart = echarts.init(document.getElementById(this.moduleParam.id))
					myChart.setOption(newValue)
				}
			},
			upmoduleParam(newvalue, oldvalue) {
				if (newvalue) {
					this.moduleParamEcharts = newvalue;
				}
			},
			onClick(event, ownerInstance) {
				if (!this.clickData) {
					return
				}
				ownerInstance.callMethod('onViewClick', {
					value: this.clickData.value,
					name: this.clickData.name,
					dataIndex: this.clickData.dataIndex,
					seriesName: this.clickData.seriesName
				})
				// 上次点击数据置空
				this.clickData = null
			}
		}
	}
</script>

<style>
	.echrts_content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 100%;
	}
</style>